<html> 
 
<head> 
<title>002 | Scene | J3D</title> 

<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> 

<link rel="stylesheet" href="common/common.css"/>
<script type="text/javascript" src="common/common.js"></script>

<script type="text/javascript" src="../build/j3d.js"></script>

<script>
	var mx = 0, my = 0;
	var jsonScene;
	var root, camera, whiteMat;
	var rings = [];
	
	function init() {
		if(!checkWebGL()) return;

		engine = new J3D.Engine();
		
		J3D.Loader.loadJSON("models/text.json", function(jsmeshes) { 
			J3D.Loader.loadJSON("models/textScene.json", function(jsscene) { 
				jsonScene = jsscene;
				J3D.Loader.parseJSONScene(jsscene, jsmeshes, engine);
				
				root = engine.scene.find("root");
                camera = engine.scene.find("camera");

				rings[0] = engine.scene.find("root/ring");
				rings[1] = engine.scene.find("root/ring/ring");
				rings[2] = engine.scene.find("root/ring/ring/ring");
				rings[3] = engine.scene.find("root/ring/ring/ring/ring");
				rings[4] = engine.scene.find("root/ring/ring/ring/ring/ring");
				rings[5] = engine.scene.find("root/ring/ring/ring/ring/ring/ring");
				rings[6] = engine.scene.find("root/ring/ring/ring/ring/ring/ring/ring");

                for(var i = 0; i < 7; i++) {
                    rings[i].originalRenderer = rings[i].renderer;
                }

				draw();
			})
		});

        whiteMat = J3D.BuiltinShaders.fetch("Phong");
		whiteMat.color = J3D.Color.white;

		document.onmousemove = onMouseMove;
	}
	
	function onMouseMove(e) {
		mx = e.clientX;
		my = e.clientY;
	}
	
	function draw() {
		var sx = (( mx / window.innerWidth  ) * 2 - 1) * 0.25;
		var sy = (( my / window.innerHeight ) * 2 - 1) * -0.25;

        var r = J3D.Ray.fromMousePosition(mx, my, camera);

        for(var i = 0; i < 7; i++) {
            var h = J3D.Intersection.rayTest(r, rings[i]);
            rings[i].renderer = (h) ? whiteMat : rings[i].originalRenderer;
            rings[i].rotation.y = sx + (sx * i * 0.02);
            rings[i].rotation.x = sy + (sy * i * 0.02);
        }

        root.rotation.y = sx;

		engine.render();
        requestAnimationFrame(draw);
	}
		
</script>

</head> 

<body onload="init();">
	<script>
		setLabels("002. Unity3d scene", "A simple scene with lights, camera and meshes<br>exported from Unity3d");
	</script>
	
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-24294554-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</body> 

</html> 